package com.smart.wap.move;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.domain.LoginResult;
import com.smart.manager.pop.entities.models.PopDisInviter;
import com.smart.utils.WorkStp;
import com.smart.wap.bodys.PopLoginBody;
import com.smart.wap.bodys.WorkLoginBody;
import com.smart.wap.service.WebsiteService;
import com.smart.wap.service.WorkLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 会员资料信息接口
 *
 * @author Administrator
 */
@Api(tags = "渠道端-身份认证")
@RestController
@RequestMapping(SmartConstant.WAP_API_PREFIX + "move/login")
@RequiredArgsConstructor
@Log4j2
public class MoveLoginController {

    private final WorkLoginService workLoginService;
    private final WebsiteService websiteService;
    @PostMapping(value = "getLoginCode")
    @ApiOperation(value = "获取手机登录验证码", notes = SmartConstant.NOT_NEED_TOKEN)
    public AjaxResult<String> getSmsCode(@RequestBody @Validated PopLoginBody.authCodeBody body) {
        websiteService.getAuthCode(body);
        return AjaxResult.success();
    }


    @ApiOperation(value = "微信授权跳转")
    @GetMapping("login")
    public void  wxLogin(HttpServletResponse response,@RequestParam(required = false,name = "acting") String acting) {
        log.warn("微信授权跳转:  acting == {}",acting);
        websiteService.wxLogin(response, acting);
    }


    @ApiOperation(value = "微信授权跳转(v2)")
    @GetMapping("login2")
    public AjaxResult<String>  login2(@RequestParam(required = false,name = "acting") String acting) {
        log.warn("微信授权跳转(v2):  acting == {}",acting);
        return websiteService.wxLogin2(acting);
    }


    @PostMapping(value = "wxLogin")
    @ApiOperation(value = "微信授权码登录", notes = SmartConstant.NOT_NEED_TOKEN )
    public AjaxResult<LoginResult> getAccessToken(@RequestBody @Validated PopLoginBody.LoginBody body) {
        log.warn("微信授权登录! = {}",body);
        LoginResult accessToken = websiteService.getAccessToken(body);
        log.warn("accessToken = {}" , accessToken);
        return AjaxResult.data(accessToken);
    }


    @ApiOperation(value = "微信获取用户信息")
    @GetMapping("infoUser")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<PopDisInviter> getInfoUser() {
        PopDisInviter account = websiteService.getAccount();
        return AjaxResult.data(account);
    }



    @PostMapping(value = "smsLogin")
    @ApiOperation(value = "手机验证码登录", notes = SmartConstant.NOT_NEED_TOKEN)
    public AjaxResult<LoginResult> actionSmsLogin(@RequestBody @Validated PopLoginBody.authCodeLoginBody body) {
        LoginResult loginResult = websiteService.authCodeLogin(body);
        return AjaxResult.data(loginResult);
    }

    @PostMapping(value = "refreshToken")
    @ApiOperation(value = "刷新身份令牌", notes = SmartConstant.NOT_NEED_TOKEN + "4002 错误码调用")
    public AjaxResult<LoginResult> refreshToken(@RequestBody @Validated WorkLoginBody.RefreshTokenBody body) {
        LoginResult loginResult = workLoginService.refreshToken(body);
        return AjaxResult.data(loginResult);
    }

    @ApiOperation(value = "注销登录")
    @PostMapping("logout")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<String> logout() {
        if (WorkStp.isLogin()) {
            WorkStp.logout();
        }
        return AjaxResult.success();
    }
}
